<template>
  <transition name="el-zoom-in-center">
    <div class="jsbos-preview-main">
      <div class="jsbos-common-page-header">
        <el-page-header
          @back="goBack"
          :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
        />
        <div class="options">
          <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
            保 存
          </el-button>
          <el-button @click="goBack"> 取 消 </el-button>
        </div>
      </div>
      <el-row
        :gutter="15"
        class="main"
        :style="{ margin: '0 auto', width: '1200px' }"
      >
        <el-form
          v-loading="loading"
          ref="elForm"
          :model="dataForm"
          :rules="rules"
          size="small"
          label-width="100px"
          label-position="right"
        >
          <template v-if="!loading">
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="基本信息">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="计划名称" prop="name">
                <el-input
                  v-model="dataForm.name"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="关联需求征集" prop="demandCollectId">
                <custom-select
                  v-model="dataForm.demandCollectId"
                  placeholder="请选择"
                  clearable
                >
                </custom-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="关联需求" prop="demandId">
                <custom-select
                  v-model="dataForm.demandId"
                  placeholder="请选择"
                  clearable
                >
                </custom-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="主办单位" prop="organizer">
                <company-select
                  v-model="dataForm.organizer"
                  placeholder="请选择"
                  clearable
                >
                </company-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="承办单位" prop="organizerBy">
                <company-select
                  v-model="dataForm.organizerBy"
                  placeholder="请选择"
                  clearable
                >
                </company-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="开始时间" prop="startDatetime">
                <el-date-picker
                  v-model="dataForm.startDatetime"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="datetime"
                  format="yyyy-MM-dd HH:mm"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="截至时间" prop="endDatetime">
                <el-date-picker
                  v-model="dataForm.endDatetime"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="datetime"
                  format="yyyy-MM-dd HH:mm"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="培训预算" prop="budget">
                <Amount
                  v-model="dataForm.budget"
                  placeholder="金额文本"
                  :step="1"
                  controls-position="right"
                >
                </Amount>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="计划类型" prop="type">
                <el-select
                  v-model="dataForm.type"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in typeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="描述" prop="description">
                <el-input
                  v-model="dataForm.description"
                  placeholder="请输入"
                  :style="{ width: '100%' }"
                  true
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="附件" prop="files">
                <UploadFz
                  v-model="dataForm.files"
                  :fileSize="5"
                  sizeUnit="MB"
                  :limit="9"
                  buttonText="点击上传"
                >
                </UploadFz>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="计划明细">
                  <template slot="other">
                    <div style="float: right">
                      <el-button
                        type="text"
                        icon="el-icon-plus"
                        @click="addTemsPlanDetailForm()"
                        >添加</el-button
                      >
                    </div>
                  </template>
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <el-table :data="dataForm.temsPlanDetailList" size="mini">
                  <el-table-column
                    type="index"
                    width="50"
                    label="序号"
                    align="center"
                  />
                  <el-table-column prop="name" label="培训名称">
                  </el-table-column>
                  <el-table-column prop="budget" label="培训预算">
                  </el-table-column>
                  <el-table-column prop="startDatetime" label="开始时间">
                  </el-table-column>
                  <el-table-column prop="endDatetime" label="截至时间">
                  </el-table-column>
                  <el-table-column prop="address" label="培训地点">
                  </el-table-column>
                  <el-table-column prop="lecturerUserId" label="讲师">
                  </el-table-column>
                  <el-table-column prop="curator" label="负责人">
                  </el-table-column>
                  <el-table-column prop="courseFaceId" label="面授课程">
                  </el-table-column>
                  <el-table-column prop="sourceFunding" label="经费来源">
                  </el-table-column>
                  <el-table-column prop="classHour" label="课时">
                  </el-table-column>
                  <el-table-column label="操作" width="50">
                    <template slot-scope="scope">
                      <el-button
                        size="mini"
                        type="text"
                        class="jsbos-table-delBtn"
                        @click="delTemsPlanDetailList(scope.$index)"
                      >
                        删除
                      </el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label-width="0">
                <groupTitle content-position="left" content="计划监督人">
                </groupTitle>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="监督人" prop="supervisor">
                <user-select
                  v-model="dataForm.supervisor"
                  placeholder="请选择"
                  clearable
                  :multiple="true"
                >
                </user-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="状态" prop="enabledMark">
                <el-switch
                  v-model="dataForm.enabledMark"
                  :active-value="1"
                  :inactive-value="0"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="实施状态" prop="enforceMark">
                <el-select
                  v-model="dataForm.enforceMark"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in enforceMarkOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </template>
        </el-form>
      </el-row>
      <detail-form
        v-if="detailFormVisible"
        ref="detailForm"
        @refresh="addTemsPlanDetailList"
      >
      </detail-form>
    </div>
  </transition>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";

import DetailForm from "./PlanDetailForm.vue";
export default {
  components: { DetailForm },
  props: [],
  data() {
    return {
      detailFormVisible: false,
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        name: "",
        demandCollectId: "",
        demandId: "",
        organizer: "",
        organizerBy: "",
        startDatetime: "",
        endDatetime: "",
        budget: "",
        type: "",
        description: "",
        files: [],
        supervisor: [],
        extendFields: "",
        enabledMark: 0,
        enforceMark: "10",
        creatorTime: "",
        creatorUserId: "",
        lastModifyUserId: "",
        lastModifyTime: "",
        temsPlanDetailList: [],
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
      typeOptions: [
        { fullName: "年度", id: "1" },
        { fullName: "月度", id: "2" },
        { fullName: "季度", id: "3" },
      ],
      enforceMarkOptions: [
        { fullName: "待实施", id: "10" },
        {
          fullName: "实施中",
          id: "20",
        },
        { fullName: "已实施", id: "30" },
        { fullName: "已归档", id: "40" },
      ],

      sourceFundingOptions: [
        { fullName: "教育经费", id: "1" },
        { fullName: "行政经费", id: "2" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    temsPlanDetailExist() {
      let isOk = true;
      for (let i = 0; i < this.dataForm.temsPlanDetailList.length; i++) {
        const e = this.dataForm.temsPlanDetailList[i];
      }
      return isOk;
    },
    goBack() {
      this.$emit("refresh");
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
      this.dataForm.enforceMark = "10";
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/course/TemsPlan/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          if (!this.temsPlanDetailExist()) return;
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/course/TemsPlan",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/course/TemsPlan/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },

    addTemsPlanDetailForm() {
      this.detailFormVisible = true;
      this.$nextTick(() => {
        this.$refs["detailForm"].init();
      });
    },
    addTemsPlanDetailList(item) {
      if (item) {
        this.dataForm.temsPlanDetailList.push(item);
      }
      this.detailFormVisible = false;
    },
    delTemsPlanDetailList(index) {
      this.dataForm.temsPlanDetailList.splice(index, 1);
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.files = JSON.stringify(_data.files);
      _data.supervisor = JSON.stringify(_data.supervisor);
      _data.enabledMark = parseInt(_data.enabledMark);
      for (let i = 0; i < _data.temsPlanDetailList.length; i++) {
        var _list = _data.temsPlanDetailList[i];
      }
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.files = JSON.parse(_dataAll.files);
      _dataAll.supervisor = JSON.parse(_dataAll.supervisor);
      _dataAll.enabledMark = parseInt(_dataAll.enabledMark);
      for (let i = 0; i < _dataAll.temsPlanDetailList.length; i++) {
        var _list = _dataAll.temsPlanDetailList[i];
      }
      this.dataForm = _dataAll;
    },
  },
};
</script>
